home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / hills01.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  3.1 KB  |  93 lines

  1. ' Height map loader     
  2. dim file                            ' File handle
  3. dim xsize, ysize                    ' Map dimensions
  4. dim x, y, value$, value#            ' Working variables
  5.  
  6. ' Open file
  7. file = OpenFileRead ("files\hills01.dat")
  8. gosub CheckError
  9.  
  10. ' First 2 lines contain dimensions
  11. xsize = val (ReadLine (file))       ' Read line reads a line. Val converts text to number.
  12. ysize = val (ReadLine (file))
  13.  
  14. ' Allocate heightmap array
  15. dim h#(xsize)(ysize)
  16.  
  17. ' Read in lines
  18. for y = 1 to ysize
  19.     for x = 1 to xsize
  20.         h#(x)(y) = val (ReadText (file, x = 1)) * 10
  21.         gosub CheckError 
  22.     next
  23. next
  24. CloseFile (file)
  25.  
  26. dim texture: texture = LoadMipmapTexture ("textures\00005.jpg")
  27. glEnable (GL_TEXTURE_2D)
  28. glBindTexture (GL_TEXTURE_2D, texture)   
  29.  
  30. ' Light map
  31. dim l#(x)(y), v#(4)(2), light#(2), temp#
  32. light# = Normalize (vec3 (-1, -1, 1))
  33. for y = 1 to ysize
  34.     for x = 1 to xsize
  35.         v#(1) = vec3 (-1, h#((x - 2) % xSize + 1)((y - 2) % ySize + 1), -1)
  36.         v#(2) = vec3 ( 1, h#((x    ) % xSize + 1)((y - 2) % ySize + 1), -1)
  37.         v#(3) = vec3 ( 1, h#((x    ) % xSize + 1)((y    ) % ySize + 1),  1)
  38.         v#(4) = vec3 (-1, h#((x - 2) % xSize + 1)((y    ) % ySize + 1),  1)
  39.         temp# = Normalize (CrossProduct (v#(3)-v#(1), v#(2)-v#(4))) * -light#
  40.         if temp# < 0 then
  41.             temp# = 0
  42.         endif
  43.         l#(x)(y) = temp# * temp# * temp# + .2
  44.     next
  45. next
  46.  
  47.  
  48. ' Render map to screen
  49. const texScale# = .1
  50. dim yangle#, xangle#, texXOffs#, texYOffs#, hills
  51.  
  52. hills = glGenLists (1)
  53. glNewList (hills, GL_COMPILE)
  54.     for y = 1 to ysize - 1
  55.         texYOffs# = y * texScale#
  56.         glBegin (GL_TRIANGLE_STRIP)
  57.         for x = 1 to xsize - 1            
  58.             texXOffs# = x * texScale#  
  59.                 temp# = l#(x)(y)
  60.                 glColor3f (temp#, temp#, temp#): glTexCoord2f (texXOffs#,            texYOffs#):             glVertex3f (x  , h#(x)  (y)  , y)
  61. '                glColor3fv (vec3 (1,1,1) * l#(x % xSize + 1)(y)): glTexCoord2f (texXOffs#+texScale#,  texYOffs#):             glVertex3f (x+1, h#(x+1)(y)  , y)
  62. '                glColor3fv (vec3 (1,1,1) * l#(x % xSize + 1)(y % ySize + 1)): glTexCoord2f (texXOffs#+texScale#,  texYOffs#+texScale#):   glVertex3f (x+1, h#(x+1)(y+1), y+1)
  63.                 temp# = l#(x)(y % ySize + 1)
  64.                 glColor3f (temp#, temp#, temp#): : glTexCoord2f (texXOffs#,            texYOffs#+texScale#):   glVertex3f (x  , h#(x)  (y+1), y+1)
  65.         next
  66.         glEnd ()
  67.     next
  68. glEndList ()
  69.     
  70. while true
  71.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)    ' Clear screen
  72.     glLoadIdentity ()
  73.     glTranslatef (0, 0, -50)
  74.     glRotatef (xangle#, 1, 0, 0)
  75.     glRotatef (yangle#, 0, 1, 0)
  76.     glTranslatef (-xsize / 2, 0, -ysize / 2)
  77.  
  78.     glCallList (hills)
  79.     ' Display finished result
  80.     SwapBuffers ()              
  81.  
  82.     ' Mouse input
  83.     yangle# = yangle# + mouse_xd () * 100
  84.     xangle# = xangle# + mouse_yd () * 100
  85. wend
  86.  
  87. end
  88. CheckError:
  89.     if FileError () <> "" then
  90.         print FileError ()
  91.         end
  92.     endif
  93.     return